package com.weishi.project.training.controller;

import java.util.Date;
import java.util.List;

import com.weishi.project.sport.trainingdata.vo.TypeValueVO;
import com.weishi.project.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.weishi.framework.aspectj.lang.annotation.Log;
import com.weishi.framework.aspectj.lang.enums.BusinessType;
import java.util.Arrays;
import java.util.Map;

import com.weishi.project.training.domain.TrainingLocation;
import com.weishi.project.training.service.ITrainingLocationService;
import com.weishi.framework.web.controller.BaseController;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.common.utils.poi.ExcelUtil;
import com.weishi.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 */
@Api(value = "训练地点",description = "训练地点")
@RestController
@RequestMapping("/training/trainingLocation")
public class TrainingLocationController extends BaseController {

    @Autowired
    private ITrainingLocationService trainingLocationService;


    /**
     * 查询训练地点列表
     */
    @PreAuthorize("@ss.hasPermi('training:location:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrainingLocation trainingLocation) {
        startPage();
        List<TrainingLocation> list = trainingLocationService.selectTrainingLocationList(trainingLocation);
        return getDataTable(list);
    }

    /**
     * 导出训练地点列表
     */
    @PreAuthorize("@ss.hasPermi('training:location:export')")
    @Log(title = "导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TrainingLocation trainingLocation) {
        List<TrainingLocation> list = trainingLocationService.selectTrainingLocationList(trainingLocation);
        ExcelUtil<TrainingLocation> util = new ExcelUtil<TrainingLocation>(TrainingLocation.class);
        return util.exportExcel(list, "location");
    }

    /**
     * 获取训练地点详细信息
     */
    @PreAuthorize("@ss.hasPermi('training:location:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(trainingLocationService.getInfo(id));
    }

    /**
     * 新增训练地点
     */
    @PreAuthorize("@ss.hasPermi('training:location:add')")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrainingLocation trainingLocation) {
        Map<String,Double> map = trainingLocationService.getLngAndLat(trainingLocation);
        trainingLocation.setLng(map.get("lng"));
        trainingLocation.setLat(map.get("lat"));
        if(trainingLocation.getCtime()==null)trainingLocation.setCtime(new Date());
        trainingLocationService.save(trainingLocation);
        return AjaxResult.success();
    }

    /**
     * 修改训练地点
     */
    @PreAuthorize("@ss.hasPermi('training:location:edit')")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainingLocation trainingLocation) {
        Map<String,Double> map = trainingLocationService.getLngAndLat(trainingLocation);
        trainingLocation.setLng(map.get("lng"));
        trainingLocation.setLat(map.get("lat"));
        trainingLocationService.updateById(trainingLocation);
        return AjaxResult.success();
    }

    /**
     * 删除训练地点
     */
    @PreAuthorize("@ss.hasPermi('training:location:remove')")
    @Log(title = "删除", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        trainingLocationService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }
    
}